package com.qf.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication
@ComponentScan("com.qf")
@MapperScan("com.qf.dao")
public class WMUApplication {

    public static void main(String[] args) {
        SpringApplication.run(WMUApplication.class, args);
    }

    /**
     * 全局跨域的过滤器
     */
    @Bean
    public CorsFilter getCorsFilter(){

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*"); // 允许任意请求头跨域
        corsConfiguration.addAllowedOrigin("*"); // 允许任意的来源的请求
        corsConfiguration.addAllowedMethod("*"); // 允许任意方法类型
        corsConfiguration.setAllowCredentials(true); // 允许cookie跨域

        UrlBasedCorsConfigurationSource corsConfigurationSource
                = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(corsConfigurationSource);
    }

}
